﻿using System;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Bases;

namespace Fuse8.ContentManagementFramework.BizFacade
{
    public static class SiteAwareEntityObjectMediator
    {
        public static ICmsContext ApplicationContext
        {
            get
            {
                return DependencyServiceLocator.Current.GetInstance<ICmsContext>();
            }
        }

        public static void SetSiteEntity(ISiteAwareEntityObject entity)
        {
            if (entity != null)
            {
                var cmsSite = ApplicationContext.ContextSite;

                Guard.ArgumentNotNull(cmsSite, "Context site");

                entity.SiteId = cmsSite.Id;
                entity.SiteGroupId = cmsSite.SiteGroupId;
            }
        }

        public static void SetSiteEntityByCategory(ISiteAwareEntityObject entity, SiteAwareCategory category)
        {
            if ((entity != null) && (category != SiteAwareCategory.NotDefined))
            {
                var cmsSite = ApplicationContext.ContextSite;

                Guard.ArgumentNotNull(cmsSite, "Context site");

                switch (category)
                {
                    case SiteAwareCategory.Site:
                        entity.SiteId = cmsSite.Id;
                        entity.SiteGroupId = cmsSite.SiteGroupId;
                        break;
                    case SiteAwareCategory.SiteGroup:
                        entity.SiteId = new Nullable<int>();
                        entity.SiteGroupId = cmsSite.SiteGroupId;
                        break;
                    case SiteAwareCategory.AllSiteGroups:
                        entity.SiteId = new Nullable<int>();
                        entity.SiteGroupId = new Nullable<int>();
                        break;
                }
            }
        }
    }
}
